<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test netty-webSocket client</title>
</head>


<body>
    <script type="text/javascript">
        var socket;
        var wsUri = "ws://localhost:8899/ws";
        if(window.WebSocket){
            socket = new WebSocket(wsUri);
            console.log(socket.readyState)
            //服务器有回传消息触发
            socket.onmessage = function (event) {
                var ta = document.getElementById("responseText");
                ta.value = ta.value + "\r\n" + event.data;
            }
            //连接建立就触发
            socket.onopen = function (ev) {
                var ta = document.getElementById("responseText");
                ta.value = "连接开启！";
            }
            //连接关闭触发
            socket.onclose = function (ev) {
                var ta = document.getElementById("responseText");
                ta.value = ta.value + "连接断开！";
            }


        }else{
            alert("浏览器不支持websocket")
        }

        function send(message) {
            if(!window.WebSocket){
                return;
            }
            if(socket.readyState == WebSocket.OPEN){
                socket.send(message)
            }else{
                alert("连接未开启")
            }

        }
    </script>
    <form  onsubmit="return false;">

        <textarea name="message" style="width: 200px; height: 200px"></textarea>
        <input type="button" value="发送数据" onclick="send(this.form.message.value)"/>

        <h3>服务端输出</h3>
        <textarea id="responseText" style="width: 200px; height: 200px"></textarea>
        <input type="button" value="清空内容" onclick="javascript:document.getElementById('responseText').value=''"/>
    </form>
</body>

</html>